/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.fedoraifz.wadl.fedora34;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;

import javax.activation.DataSource;
import javax.mail.util.ByteArrayDataSource;
import javax.xml.bind.JAXBContext;
//import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;

import org.igfay.jfig.JFig;
import org.igfay.jfig.JFigException;
import org.jvnet.ws.wadl.util.DSDispatcher;
//import org.jvnet.ws.wadl.util.JAXBDispatcher;
import org.jvnet.ws.wadl.util.UriBuilder;
import org.apache.log4j.Logger;

import uk.ljmu.fedoraifz.restrequests.UserCredentials;

public class Objects {
    private static Logger log = Logger.getLogger(Objects.class);
    private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
    //private JAXBDispatcher _jaxbDispatcher;
    private DSDispatcher _dsDispatcher;
    private UriBuilder _uriBuilder;
    @SuppressWarnings("unused")
    private JAXBContext _jc;
    private HashMap<String, Object> _templateAndMatrixParameterValues;
    private DataSource retVal;
    private List<String> matrixParamSet;

    public void setRetVal(DataSource retVal) {
	this.retVal = retVal;
    }

    public DataSource getRetVal() {
	return retVal;
    }

    /**
     * @param matrixParamSet the matrixParamSet to set
     */
    public void setMatrixParamSet(List<String> matrixParamSet) {
	this.matrixParamSet = matrixParamSet;
    }

    /**
     * @return the matrixParamSet
     */
    public List<String> getMatrixParamSet() {
	return matrixParamSet;
    }

    /**
     * Create new instance
     * 
     */
    public Objects()
    throws JAXBException
    {
	_dsDispatcher = new DSDispatcher();
	_uriBuilder = new UriBuilder();
	@SuppressWarnings("unused")
	List<String> _matrixParamSet;
	String basePath;
	try {
	    basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	} catch (JFigException e) {
	    log.fatal(e.getMessage());
	    //Error, use default Fedora path
	    basePath = "http://localhost:8080/fedora";
	}
	setMatrixParamSet(_uriBuilder.addPathSegment(basePath));
	setMatrixParamSet(_uriBuilder.addPathSegment("/objects"));
	_templateAndMatrixParameterValues = new HashMap<String, Object>();
    }

    public void get()
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    public void get(UserCredentials userCreds)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());

	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    /*public void getAsvoid()
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void get(String terms, String query, Integer maxresults, String sessiontoken, String resultformat)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	if (terms != null && !terms.equals(""))
	    _queryParameterValues.put("terms", terms);
	if (query != null && !query.equals(""))
	    _queryParameterValues.put("query", query);
	if (maxresults != null && !maxresults.equals(""))
	    _queryParameterValues.put("maxResults", maxresults);
	if (sessiontoken != null && !sessiontoken.equals(""))
	    _queryParameterValues.put("sessionToken", sessiontoken);
	if (resultformat != null && !resultformat.equals(""))
	    _queryParameterValues.put("resultFormat", resultformat);

	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    public void get(UserCredentials userCreds, String terms, String query, Integer maxresults, String sessiontoken, String resultformat)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());

	if (terms != null && !terms.equals(""))
	    _queryParameterValues.put("terms", terms);
	if (query != null && !query.equals(""))
	    _queryParameterValues.put("query", query);
	if (maxresults != null && !maxresults.equals(""))
	    _queryParameterValues.put("maxResults", maxresults);
	if (sessiontoken != null && !sessiontoken.equals(""))
	    _queryParameterValues.put("sessionToken", sessiontoken);
	if (resultformat != null && !resultformat.equals(""))
	    _queryParameterValues.put("resultFormat", resultformat);

	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    /* public void getAsvoid(String terms, String query, Integer maxresults, String sessiontoken, String resultformat)
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	_queryParameterValues.put("terms", terms);
	_queryParameterValues.put("query", query);
	_queryParameterValues.put("maxResults", maxresults);
	_queryParameterValues.put("sessionToken", sessiontoken);
	_queryParameterValues.put("resultFormat", resultformat);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public static class NextPID {

	//private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Create new instance
	 * 
	 */
	public NextPID()
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    matrixParamSet = _uriBuilder.addPathSegment("/objects/");
	    matrixParamSet = _uriBuilder.addPathSegment("nextPID");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	}

	public void post()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doPOST(new ByteArrayDataSource("", ""), DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null);
	    return ;
	}

	public void post(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    _queryParameterValues.put("format", "xml");
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    this.retVal = _dsDispatcher.doPOST(new ByteArrayDataSource("", ""), DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null);
	    return ;
	}

	/*public void postAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doPOST(null, null, _url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void post(Integer numpids, String namespace, String format)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (numpids != null && !numpids.equals(""))
		_queryParameterValues.put("numPIDs", numpids);
	    if (namespace != null && !namespace.equals(""))
		_queryParameterValues.put("namespace", namespace);
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doPOST(new ByteArrayDataSource("", ""), DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null);
	    return ;
	}

	public void post(UserCredentials userCreds, Integer numpids, String namespace, String format)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (numpids != null && !numpids.equals(""))
		_queryParameterValues.put("numPIDs", numpids);
	    if (namespace != null && !namespace.equals(""))
		_queryParameterValues.put("namespace", namespace);
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);  
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doPOST(new ByteArrayDataSource("", ""), DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null);
	    return ;
	}

	/*public void postAsvoid(Integer numpids, String namespace, String format)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("numPIDs", numpids);
	    _queryParameterValues.put("namespace", namespace);
	    _queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doPOST(null, null, _url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

    }

}
